package shop.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.http.Part;

import shop.dao.UserDao;
import shop.dao.impl.UserDaoImpl;
import shop.entity.User;

@MultipartConfig
@WebServlet("/FaceServlet")
public class FaceServlet extends HttpServlet {
	
	private UserDao dao = new UserDaoImpl();

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//获得Part对象
		Part part = request.getPart("face");
		//获得上传文件名
		String name = part.getSubmittedFileName();
		//存储路径
		//String path = request.getServletContext().getRealPath("/images/"+name);
		String path = "d:/images/"+name;
		System.out.println(path);
		//写入文件
		part.write(path);
		
		
		
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		user.setFace(name);
		dao.changeFace(user.getId(),name);
		session.setAttribute("user", user);
		response.sendRedirect("member.jsp");
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//获得Part对象
		doGet(request, response);
	}


}
